/****
@滚动锚点定位功能
@锚点句柄固定到窗口顶部
@对应内容出现时标记锚点高亮
@基于JQ
@杨永
@2015-01-20
@QQ:377746756
@结构说明
<div class="title-top">
	<div class="js_fix_bar">
		<a href="javascript:void(0);">·魅惑女人香</a>
		<a href="javascript:void(0);">·激情男人味</a>
		<a href="javascript:void(0);">·经典的见证</a>
		<a href="javascript:void(0);">·套装精选</a>
	</div>
</div>
<div class="js_scroll_inset">
	<div class="js_fix_to_con"  class="list">A</div>
	<div class="js_fix_to_con"  class="list">B</div>
	<div class="js_fix_to_con" class="list">C</div>
	<div class="js_fix_to_con" class="list">D</div>
</div>
****/
;(function($){
	var ScrollFixBar = function(fixBarNode){
		var _this_ = this;
		//保存单个滚动对象
		this.scrollWrap          = fixBarNode;
		//获取距离页面的顶部距离和最大滚动范围
		this.fixWrapOffsetTop    = this.scrollWrap.offset().top;
		this.fixWrapOffsetMaxTop = this.scrollWrap.offset().top+this.scrollWrap.height();
		//获取固定fixBar和锚点 
		this.fixBar         = this.scrollWrap.find("div.js_fix_bar");
		this.anchorList     = this.fixBar.find("a");
		this.fixBarHeight   = this.fixBar.height();
		//获取锚点对应的滚动区域
		this.scrollIntoCons = this.scrollWrap.find("div.js_fix_to_con");
		//滚动的距离如果出现在范围内
		$(window).scroll(function(){
			_this_.scrollInit();
		});
		//锚点点击定位
		this.bodyNode  = $("body,html");
		this.anchorList.click(function(){
			var _this  = $(this);
				index  = $(this).index();
			_this_.bodyNode.animate({scrollTop:_this_.scrollIntoCons.eq(index).offset().top-_this_.fixBarHeight},function(){
				window.setTimeout(function(){_this.addClass("hover").siblings().removeClass("hover");},200);
			});
		});
		//初始化执行一次
		this.scrollInit();
	};
	ScrollFixBar.prototype={
		scrollInit:function(){
			var _this_ = this;
			this.scrollTopVal = document.documentElement.scrollTop||document.body.scrollTop||window.pageYOffset||0;
			//如果滚动的值在范围内
			if(this.scrollTopVal>=this.fixWrapOffsetTop+this.fixBarHeight&&this.fixBarHeight<=this.fixWrapOffsetMaxTop+this.scrollTopVal){
				//固定
				this.fixBar.addClass("fix-bar-pos");
				var  i = 0;
				//记录当前索引
				this.scrollIntoCons.each(function(){
					var offsetTopVal = $(this).offset().top;
					if(_this_.scrollTopVal>=offsetTopVal){
						i = $(this).index();
					};
				});
				//标记高亮 
				this.anchorList.eq(i).addClass("hover").siblings().removeClass("hover");
			}else{
				//解除
				this.fixBar.removeClass("fix-bar-pos");
				this.anchorList.removeClass("hover");
			};
		}	
	};
	ScrollFixBar.init = function(fixBarNodes){
		var _this_ = this;
		fixBarNodes.each(function(){
			new _this_($(this));
		});	
	};
	//注册到全局
	window["ScrollFixBar"] = ScrollFixBar;
})(jQuery);